<!--
* @description 
* @fileName login.vue
* @author huangyu
* @date 2022/05/05 09:58:58
!-->
<template>
    <div style="margin-top:100px;width:650px;">
        <el-form :model="ruleForm" label-width="100px" :rules="rules" ref="ruleForm">
            <el-form-item label="账号" prop="userAccount">
                <el-input v-model="ruleForm.userAccount"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input type="password" v-model="ruleForm.password"></el-input>
            </el-form-item>
            <el-form-item label="">
                <el-button type="primary" @click="login('ruleForm')">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import * as types from "@/store/types";
import md5 from 'js-md5';
export default {
    components: {},
    data() {
        return {
            id: "",
            ruleForm: {
                userAccount: "admin",
                password: "123456",
            },
            rules:{},
            app: this.$cloudbase,
        }
    },
    mounted() {
    },
    methods: {
        login(){
            let params = {
                type: 'getLogin',
                data: {
                    userAccount: this.ruleForm.userAccount,
                    password: md5(this.ruleForm.password),
                },
            }
            this.axios
            .get('system',params).then(res=>{
                if(res){
                    console.log(res)
                    const loginState = this.app.auth({ persistence: 'none' }).getLoginState();
                    window.console.log("loginState",!loginState)
                    if(!loginState){
                        console.log("okkk222")
                        // 2. 请求开发者自有服务接口获取ticket
                        const ticket = res
                        // 3. 登录 CloudBase
                        this.app
                        .auth()
                        .customAuthProvider()
                        .signIn(ticket)
                        console.log("okkk")
                        // console.log(this.app.auth().hasLoginState())
                        let logindata = {
                            user: {
                                userAccount: res.data.UserAccout,
                                userName: res.data.UserName,
                                userId: res.data._id,
                            },
                            power: res.data.power
                        };
                        console.log(logindata)
                        this.$store.commit(types.LOGIN, logindata);
                        let redirect = decodeURIComponent(
                            this.$route.query.redirect || "/admin/default"
                        );
                        this.$router.push({path: redirect});
                        this.$message.success("登录成功!")
                    }
                }else{
                    this.$message.error("账号/密码不正确")
                }
            })
        },
    },
}
</script>

<style scoped>
</style>